
import pygame
from sys import exit
from pygame.locals import *


pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)



size = width, height = 640,480

screen = pygame.display.set_mode(size)
pygame.display.set_caption("jingziqi")


clock = pygame.time.Clock()

upImageFilename = pygame.image.load("images/kaishi.png")
downImageFilename = pygame.image.load("images/xinkaishi.png")


empty,x ,o = 0, 1, 2
board = [[empty] * 3,[empty] * 3,[empty] * 3]

board[1][1] = 1
board[0][1] = 2
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit(0)
    screen.fill(white)
    pygame.draw.rect(screen,white,(0,0,width,height))
    pygame.draw.line(screen,black,(0,165),(640,165),5)
    pygame.draw.line(screen,black,(0,325),(640,325),5)
    pygame.draw.line(screen,black,(200,0),(200,640),5)
    pygame.draw.line(screen,black,(440,0),(440,640),5)


    for row, line in enumerate(board):
        for col, val in enumerate(line):
            if val == x:
                upper_left = (col * 240 + 5,row * 20 + 155)
                lower_right = (col * 265 + 155, row * 300 + 5)
                pygame.draw.line(screen,red,upper_left,lower_right,5)
                upper_right = (col * 230 + 5, row * 300 + 5)
                lower_left = (col * 420 + 5, row * 180 + 5)
                pygame.draw.line(screen, red, upper_right, lower_left, 5)
            elif val == o:

                rect = (col * 240 + 5, row * 220 + 5,150,150)
                pygame.draw.ellipse(screen, green, rect, 5)
            else:
                val == empty
                continue

    clock.tick(60)

    pygame.display.flip()

    font = pygame.font.Font(None,24)
    minutes = str(90-pygame.time.get_ticks()/1000%60)
    seconds = str(pygame.time.get_ticks()/60000)
    survivedtext = font.render(seconds+":"+minutes, True, (0 ,0 ,0))
    screen.blit(survivedtext, (500 , 8))

    
    
    pygame.display.flip()
    
    

class Button(object):
    def _init_(self, upimage, downimage,position):
        self.imageUp = pygame.image.load(upimage).convert_alpha()
        self.imageDown = pygame.image.load(downimage).convert_alpha()
        self.position = position
    def isOver(self):
        point_x,point_y = pygame.mouse.get_pos()
        x, y = self.position
        w, h = self.imageUp.get_size()

        in_x = x - w/2 < point_x < x + w/2
        in_y = y - h/2 < point_y < y + h/2
        return in_x and in_y
    def render(self):
        w, h = self.imageUp.get.size()
        x, y = self.position

        if self.isOver():
            screen.blit(self.imageDown, (x-w/2,y-h/2))
        else:
            screen.blit(self.imageUp,(x-w/2,y-h/2))
            
